<?php

namespace App\Controller\Api;

use Yolo\Di\DI;
use Yolo\IdGenerator\IdGeneratorFactory;
use Yolo\Rpc\Annotations\Api;
use Yolo\Rpc\Annotations\RpcController;
use Yolo\Service\RpcRequest;
use Yolo\Service\RpcResponse;

#[RpcController]
class TestController
{

    #[Api([
        'validation' => [
            'name' => 'required|string',
        ],
        'preProcessor' => 'getUserByIdPreProcessor',
    ])]
    public function getUserById(RpcRequest $request, RpcResponse $response): array|RpcResponse
    {
        $body = $request->getBody();
        // 你可以返回一个数组或者RpcResponse对象
//        $response->setBody(json_encode([
//            'name' => $body['name']
//        ]));
//        return $response;

        $idGenerator = DI::use(IdGeneratorFactory::class);

        return [
            'name' => $body['name'] ?? '',
            'nextId' => $idGenerator->generateId(),
        ];
    }
}
